package org.zfes.snowier.common.monitor.warn.task;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.zfes.snowier.common.monitor.biz.dao.MysqlAcutorMapper;
import org.zfes.snowier.common.monitor.biz.dao.ZActuatorMapper;
import org.zfes.snowier.common.monitor.biz.model.MysqlAcutor;
import org.zfes.snowier.common.monitor.biz.model.ZActuatorEntity;
import org.zfes.snowier.common.monitor.warn.email.IEmailService;

//@Component
public class AcutorMonitor {

    @Autowired
    private IEmailService emailService;
	@Autowired
	private ZActuatorMapper sysActuatorMapper;
	@Autowired
	private MysqlAcutorMapper mysqlAcutorMapper;
	
	private static final int expireMinits=10;
	
  @Scheduled(cron="0/30 * *  * * ? ")
  public void trackJvm(){
	 	List<ZActuatorEntity> actuatorList=sysActuatorMapper.selectList(null);
	 	Date now=new Date();
	 	List<ZActuatorEntity>  actuatorWarnList=	actuatorList.stream().filter(val->(now.getTime()-val.getDate().getTime())/(1000)/60  > expireMinits ).collect(Collectors.toList());
	 	if(actuatorWarnList!=null&&!actuatorWarnList.isEmpty()) {
	 		 String content="";
	 		for(ZActuatorEntity en: actuatorWarnList) {
	 			content+="【"+en.getInstanceKey()+"---"+new SimpleDateFormat("YYYY-MM-dd hh:mm:ss").format(en.getDate())+"】";
	 		}
	 		System.out.println("--------content:"+content);
	 		emailService.sendSimpleMail("z_huatai@163.com","心跳数据包好久没有来的JVM",content);
	 		
	 	}
  	
  }
  
  @Scheduled(cron="0/30 * *  * * ? ")
  public void trackMysql(){
		List<MysqlAcutor> actuatorList=mysqlAcutorMapper.selectList(null);
	 	Date now=new Date();
	 	List<MysqlAcutor>  actuatorWarnList=	actuatorList.stream().filter(val->(now.getTime()-val.getDate().getTime())/(1000)/60  > expireMinits ).collect(Collectors.toList());
	 	if(actuatorWarnList!=null&&!actuatorWarnList.isEmpty()) {
	 		 String content="";
	 		for(MysqlAcutor en: actuatorWarnList) {
	 			content+="【"+en.getInstanceKey()+"---"+new SimpleDateFormat("YYYY-MM-dd hh:mm:ss").format(en.getDate())+"】";
	 		}
	 		System.out.println("--------content:"+content);
	 		emailService.sendSimpleMail("z_huatai@163.com","心跳数据包好久没有来的MYSQL",content);
	 		
	 	}
	  
  	
  }
  
}
